let OrderId=0;
layui.config({
	version:'1510164124181'
}).use(['form', 'laypage', 'layer', 'element'],function(){
	form = layui.form;
	layer = layui.layer;
    laypage = layui.laypage;//分页
    element = layui.element; //元素操作
	OrderId=GetQueryString("OrderId");
	if(!isNotNull(OrderId)){
		OrderId=0;
	}else{
		SelectOrderListi();
	}

});
function GoodsDetails() {
	$("#tableInfo tbody").html("");
	$.ajax({
		url: httpurl+"/FindGoodsDetails",
		cache: false,
		headers: {
			"Access-Control-Allow-Origin": "*",
			"Access-Control-Allow-Methods": "*",
			"Access-Control-Allow-Headers": "Authorization",
			"Authorization": GetTokenInfo()
		},
		contentType : 'application/json',
		data: {
			"page":pageNo,
			"pagesize":pageRecords,
			"OrderId":OrderId,
		},
		success: function(data) {
			let dataHtml="";
			if(data.rtnCode==200){
				let initdata=data.data;
				totalRecords =initdata[initdata.length-1].total;
				for(let i=0;i<initdata.length;i++){
					dataHtml+='<tr>'+
									//'<td>'+initdata[i].id+'</td>'+
									'<td>'+initdata[i].productEntity.title+'</td>'+
									'<td>'+initdata[i].setMealEntity.meal+'</td>'+
									'<td>'+initdata[i].num+'</td>'+
									'<td>'+initdata[i].setMealEntity.newprice+'</td>'+
								'</tr>';
				}
			}else{
				totalRecords =0;
				dataHtml += '<tr>'
					+'<td colspan="12" style="text-align:center;">暂无数据</td>'
					+'</tr>';
			}
			$("#goods_detail tbody").html(dataHtml);
			layer.closeAll();
			page();
		}

	});
}

//分页
function page(){
	laypage.render({
		elem: 'page' //注意，这里的 test1 是 ID，不用加 # 号
		,count:totalRecords //数据总数，从服务端得到
		,curr: pageNo //获取起始页 location.hash.replace('#!fenye=', '')
		,hash: 'fenye' //自定义hash值
		,layout: ['count', 'prev', 'page', 'next', 'skip']
		,jump: function(obj, first){
			//首次不执行
			if(!first){
				pageNo = obj.curr;
				pageRecords = obj.limit;// 当前页面记录数
				GoodsDetails();
			}
		}
	});
}

function SelectOrderListi(){
	$.ajax({
		url: httpurl+"/FindOrderListId",
		cache: false,
		headers: {
			"Access-Control-Allow-Origin": "*",
			"Access-Control-Allow-Methods": "*",
			"Access-Control-Allow-Headers": "Authorization",
			"Authorization": GetTokenInfo()
		},
		contentType : 'application/json',
		data: {
			"id":OrderId,
		},
		// dataType: "json",
		success: function(data) {
			if(data.rtnCode==200){
				var dataInfo=data.data;
				$("#order_num").val(dataInfo.id);//订单号
				$("#type").val(dataInfo.type==0?"普通订单":(dataInfo.type==1?"秒杀订单":(dataInfo.type==2?"拼单订单":"")));//订单类型
				$("#user_names").val(dataInfo.name);//用户
				$("#address_info").val(dataInfo.address);//地址
				$("#phone").val(dataInfo.phone);//手机号

				$("#user_order_time").val(dataInfo.intime);//下单时间
				$("#order_money").val(dataInfo.ordermoney);//订单金额

				$("#couponval").val(dataInfo.couponval);//优惠券


				// $("#freight_money").val(dataInfo.isreturn)//运费
				$("#order_states option[value="+dataInfo.state+"]").attr("selected","selected");//订单状态
				$("#express_store").val(dataInfo.express_name);//快递公司
				$("#express_num").val(dataInfo.express_num);//快递单号
				// $("#express_state").val(dataInfo.isreturn);//快递状态
				// $("#express_name").val(dataInfo.isreturn);//快递员
				// $("#express_tel").val(dataInfo.isreturn)//快递员电话
				// $("#user_message").val(dataInfo.isreturn);//用户留言
				// $("#service_remark").val(dataInfo.isreturn);//客服备注
				$('.isreturn:radio:checked').val(dataInfo.isreturn);
			}
			
		}
	});
}

function UpdateOrderEntity() {
	let order_num=$("#order_num").val();//订单号
	let user_names=$("#user_names").val();//用户
	let address_info=$("#address_info").val();//地址
	let phone=$("#phone").val();//手机号

	let user_order_time=$("#user_order_time").val();//下单时间
	let order_money=$("#order_money").val();//订单金额

	let couponval=$("#couponval").val();//优惠券

	let freight_money=$("#freight_money").val()//运费
	let order_states=$("#order_states option:selected").val();//订单状态
	let express_store=$("#express_store").val();//快递公司
	let express_num=$("#express_num").val();//快递单号
	let express_state=$("#express_state").val();//快递状态
	let express_name=$("#express_name").val();//快递员
	let express_tel=$("#express_tel").val()//快递员电话
	let user_message=$("#user_message").val();//用户留言
	let service_remark=$("#service_remark").val();//客服备注
	let isreturn=$('.isreturn:radio:checked').val();
	top.layer.confirm('确认提交？',{icon: 3,title:'提示信息'},function(index){
		var index = top.layer.msg('提交中，请稍候',{icon: 16,time:false,shade:0.8});
		var httpAjax="/UpdateOrderEntity";
		if(OrderId==0){
			httpAjax="/InsertOrderInfo";
		}
		$.ajax({
			type:"POST",
			url: httpurl + httpAjax,
			cache: false,
			data: {
				"id": OrderId,
				"ordermoney": order_money,
				"sintime": user_order_time,
				"couponval": couponval,
				"address":address_info,
				"name":user_names,
				"phone":phone,
				"express_name":express_store,
				"express_num":express_num,
				"isreturn":isreturn,
				"state":order_states
			},
			// dataType: "json",
			success: function (data) {
				layer.closeAll();
				top.layer.msg(data.msg);
				re();

			}
		});
	},function(){

	});

}
function re() {
	window.location.href="OrderList.html";
}

